window.addEventListener('load', function() {
    //欢迎提示
    var lis = document.querySelectorAll('.fl li')
    var arr = decodeURI(location.search.split('=')[1])
    if (arr !== 'undefined') {
        lis[0].innerHTML = arr + ' 您好，品优购欢迎您！'
        lis[1].innerHTML = null
    } else {
        lis[0].innerHTML = '<li>品优购欢迎您！</li>'
        lis[1].innerHTML = '<a href="login.html">请登录</a> <a href = "register.html" class = "style_red" > 免费注册 </a>'
    }


    //search聚焦
    var search_text = document.querySelector('.search_text')
    document.addEventListener('keyup', function(e) {
        if (e.keyCode === 83) {
            search_text.value = null
            search_text.focus()
        }
    })


    //倒计时
    var count = document.querySelector('.count')
    var hour = document.querySelector('.hour')
    var minute = document.querySelector('.minute')
    var second = document.querySelector('.second')
    var inputTime = new Date('2022-12-24 16:00:00')
    getTime()
    var timer = setInterval(getTime, 1000)

    function getTime() {
        var date = new Date()
        var chaTime = (inputTime - date) / 1000
        if (chaTime <= 0) {
            clearInterval(timer)
        }
        var h = parseInt(chaTime / 60 / 60 % 24)
        var m = parseInt(chaTime / 60 % 60)
        var s = parseInt(chaTime % 60)

        h = h < 10 ? '0' + h : h
        m = m < 10 ? '0' + m : m
        s = s < 10 ? '0' + s : s

        hour.innerHTML = h
        minute.innerHTML = m
        second.innerHTML = s
    }


    //轮播图按钮
    var arrow_l = document.querySelector('.arrow-l')
    var arrow_r = document.querySelector('.arrow-r')
    var banner = document.querySelector('.banner')

    banner.addEventListener('mouseenter', function() {
        arrow_l.style.display = 'block'
        arrow_r.style.display = 'block'
        clearInterval(timer)
    })
    banner.addEventListener('mouseleave', function() {
        arrow_l.style.display = 'none'
        arrow_r.style.display = 'none'
        timer = setInterval(nextPic, 2500)
    })


    //小圆点
    var ul = banner.querySelector('ul')
    var ol = banner.querySelector('.banner ol')
    for (i = 0; i < ul.children.length; i++) {
        var li = document.createElement('li')
        ol.appendChild(li);
        li.setAttribute('index', i)
        li.addEventListener('click', function() {
            for (i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            this.className = 'current'
            index = this.getAttribute('index')
            animate(ul, -index * banner.offsetWidth)
        })
    }

    ol.children[0].className = 'current';
    var first = ul.children[0].cloneNode(true)
    ul.appendChild(first)
    var index = 0
    arrow_r.addEventListener('click', nextPic)

    var flag = true

    function nextPic() {
        if (flag == true) {
            flag = false
            if (index == ul.children.length - 1) {

                ul.style.left = 0
                index = 0
            }
            index++
            animate(ul, -index * banner.offsetWidth, function() {
                flag = true
            })

            for (i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            if (index == ul.children.length - 1) {
                ol.children[0].className = 'current'
            } else {
                ol.children[index].className = 'current'
            }

        }
    }

    var timer = setInterval(nextPic, 2500)

    arrow_l.addEventListener('click', function() {
        if (flag) {
            flag = false
            if (index == 0) {
                index = ul.children.length - 1
                ul.style.left = -index * banner.offsetWidth + 'px'
            }
            index--
            animate(ul, -index * banner.offsetWidth, function() {
                flag = true
            })
            for (i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            ol.children[index].className = 'current'
        }
    })

    //电梯栏
    $(function() {
        var boxTop = $(".today").offset().top;
        var flag = true
        togoTool()

        function togoTool() {
            if ($(document).scrollTop() >= boxTop) {
                $(".fixedtool").fadeIn();
            } else {
                $(".fixedtool").fadeOut();
            }
        }
        $(window).scroll(function() {
            togoTool()
            if (flag) {
                var scrollTop = $(document).scrollTop()
                $('.floor .w').each(function(i, e) {
                    var wTop = $(e).offset().top
                    if (scrollTop > wTop - 20) {
                        $('.fixedtool ul li').eq(i).addClass('current').siblings().removeClass('current')
                    }
                })
            }

        })
        $('.fixedtool ul li').click(function() {
            flag = false
            var index = $(this).index()
            $(this).addClass('current').siblings('li').removeClass()

            var current = $('.floor .w').eq(index).offset().top
            $('body, html').stop().animate({
                scrollTop: current
            }, function() {
                flag = true
            })
        })
    })
})